package com.dormitory.myt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dormitory.myt.constant.SomeContant;
import com.dormitory.myt.en.Allsushe;
import com.dormitory.myt.mapper.AllsusheMapper;
import com.dormitory.myt.service.AllsusheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AllsusheServiceImpl extends ServiceImpl<AllsusheMapper, Allsushe> implements AllsusheService {
    @Autowired
    private AllsusheService allsusheService;

    @Autowired
    private AllsusheMapper allsusheMapper;
    @Override
    public void updateNumber(Integer sushehao) {

        //1.查询该宿舍人数：
        LambdaQueryWrapper<Allsushe> queryWrapper = new LambdaQueryWrapper<>();
        //根据宿舍来查询：
        queryWrapper.eq(Allsushe::getSushehao,sushehao);
        List<Allsushe> list = allsusheService.list(queryWrapper);
        Allsushe allsushe = list.get(0);

        //2.人数加一：
        if(allsushe.getNumber() == null){
            allsushe.setNumber(1);
            LambdaUpdateWrapper<Allsushe> updateWrapper = Wrappers.<Allsushe>lambdaUpdate()
                    .eq(Allsushe::getSushehao,sushehao);
            allsusheMapper.update(allsushe, updateWrapper);

            return;
        }
       if(allsushe.getNumber() <SomeContant.MAX_NUMBER){
           allsushe.setNumber( allsushe.getNumber()+1);
           LambdaUpdateWrapper<Allsushe> updateWrapper = Wrappers.<Allsushe>lambdaUpdate()
                   .eq(Allsushe::getSushehao,sushehao);
           allsusheMapper.update(allsushe, updateWrapper);
       }




    }
}
